package com.example.databasetest;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private MyDatabaseHelper mdb;
    private Button bn,addBn,upDate,del,search;
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建SQLiteOpenhelper对象
        mdb = new MyDatabaseHelper(this,"book.db",null,2);
        bn = findViewById(R.id.create_database);
        addBn = findViewById(R.id.add_data);
        upDate = findViewById(R.id.update_data);
        del = findViewById(R.id.delete_data);
        search = findViewById(R.id.query_data);
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mdb.getWritableDatabase();
            }
        });
        addBn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 打开现有数据库
                db = mdb.getWritableDatabase();
                // db这个对象可以对数据库中的表进行增删改查
                ContentValues values = new ContentValues();
                // 组装第一条记录
                values.put("name","Day Day Up");
                values.put("author","liu");
                values.put("pages",58);
                values.put("price",30.8);
                // 插入一条数据
                db.insert("Book",null,values);
                //组装第二条数据
                values.put("name","Flowers");
                values.put("author","zhang");
                values.put("pages",30);
                values.put("price",19.9);
                // 插入第二条记录
                db.insert("Book",null,values);
                Toast.makeText(MainActivity.this,"add",Toast.LENGTH_SHORT).show();
            }
        });
        upDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取已有的数据库
                db = mdb.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("price", 10.99);
                //名字是Day Day Up的这本书的价格改成
                //10.99。
                db.update("Book", values, "name = ?", new String[] { "Day Day Up" });
            }
        });
        del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除记录
                db = mdb.getWritableDatabase();
                db.delete("Book", "pages > ?", new String[] { "30" });
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db = mdb.getWritableDatabase();
                // 查询所有记录 返回一个Cursor对象
                Cursor cursor = db.query("Book",null,null,null,null,null,null);
                if (cursor.moveToFirst()) { //指针移动到第一个位置
                    do {
                        // 遍历Cursor对象，取出数据并打印
                        @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
                        @SuppressLint("Range") String author = cursor.getString(cursor.getColumnIndex("author"));
                        @SuppressLint("Range") int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                        @SuppressLint("Range") double price = cursor.getDouble(cursor.getColumnIndex("price"));
                        Log.d("MainActivity", "book name is " + name);
                        Log.d("MainActivity", "book author is " + author);
                        Log.d("MainActivity", "book pages is " + pages);
                        Log.d("MainActivity", "book price is " + price);
                    } while (cursor.moveToNext());
                }
                cursor.close();
            }
        });

    }
}